package com.thon.service.project.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.ProjectSupportDao;
import com.thon.dao.util.Page;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectSupport;
import com.thon.entity.user.User;
import com.thon.service.project.ProjectSupportService;

@Service
@Transactional
public class ProjectSupportServiceImpl implements ProjectSupportService {

	@Autowired
	private ProjectSupportDao projectSupportDao;

	@Override
	public void add(ProjectSupport projectSupport) {

		projectSupportDao.save(projectSupport);
	}

	@Override
	public void update(ProjectSupport projectSupport) {

	}

	@Override
	public void delete(int id) {

	}

	@Override
	public ProjectSupport getProjectSupport(int id) {

		return projectSupportDao.get(id);
	}

	@Override
	public List<ProjectSupport> listSupportsByProject(int projectId) {

		return projectSupportDao.listProjectSupportsByProject(projectId);
	}

	@Override
	public Long countProjectSupport(int projectId) {

		return projectSupportDao.countSupports(projectId);
	}

	@Override
	public Long sumFundCurrent(Integer projectId) {

		return projectSupportDao.sumFundCurrent(projectId);
	}

	@Override
	public Page<ProjectSupport> findSupportsById(int projectId, int pageNo,
			int pageSize) {
		Page<ProjectSupport> page = new Page<ProjectSupport>();
		page.setPageSize(pageSize);
		page.setPageNo(pageNo);
		page.desc("createdDate");
		return projectSupportDao.findSupportsById(page, projectId);
	}

	@Override
	public Page<ProjectSupport> findSupportsByUser(int userId, int pageNo,
			int pageSize, Long start, Long end) {
		Page<ProjectSupport> page = new Page<ProjectSupport>();
		page.setPageSize(pageSize);
		page.setPageNo(pageNo);
		if (start != null && end != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return projectSupportDao.findSupportsByUser(page, userId,
					sdf.format(start), sdf.format(end));
		} else {
			return projectSupportDao.findSupportsByUser(page, userId, null,
					null);
		}
	}
	
	@Override
	public boolean approveSupport(int projectId, int userId) {
		return projectSupportDao.countSupports(projectId, userId)!=0;
	}
	@Override
	public Long sumMoney(int userId) {
		Long sum=0L;
		for(ProjectSupport ps:projectSupportDao.getProjectSupport(userId)){
			sum+=ps.getProductPrice()*ps.getProductAmount();
		}
		return sum;
	}

	@SuppressWarnings("null")
	@Override
	public Map<Integer,List<String>> listProjectSupportsByProjectId(String projectId,Date registDateStart,Date comsumptionDateStart,Date registDateEnd,Date comsumptionDateEnd,int comsumptionFee,int comsumptionTotleFee,String projectCategory){
		List<ProjectSupport> projectSupports = projectSupportDao.listProjectSupportsByProjectId(registDateStart,comsumptionDateStart,registDateEnd,comsumptionDateEnd);
		Map<Integer,Integer> userTotleAmount = new HashMap<Integer,Integer>();
		Map<Integer,Integer> userIds = new HashMap<Integer,Integer>();
		Map<Integer,String> userNames = new HashMap<Integer,String>();
		List<Integer> projectIds = new ArrayList<Integer>();
		List<Integer> projectCategoryIds = new ArrayList<Integer>();
		String[] strArray =(String[]) projectId.split(",");
		if(projectSupports!=null){
		for(int i=0;i<strArray.length;i++)
		{
			projectIds.add(Integer.parseInt(strArray[i]));
		}
		strArray = null;
		
		strArray =(String[]) projectCategory.split(",");
		
		for(int i=0;i<strArray.length;i++)
		{
			projectCategoryIds.add(Integer.parseInt(strArray[i]));
		}
		
		for(ProjectSupport projectSupport:projectSupports)
			{
			    if(projectIds.contains(0))
			    {
			    	if(projectSupport.getProject().getStatus() == Project.PROJECT_STATUS_ING) {
						Integer totAmount = projectSupport.getProduct().getPrice()
								* projectSupport.getProductAmount();
						Boolean allowPutUserId = false;
						if(projectCategoryIds.contains(0))
						{	if (totAmount >= comsumptionFee) 
								allowPutUserId = true;
						}
						else
							{if ( projectCategoryIds.contains(projectSupport.getProject().getProjectCategory().getId())&& totAmount >= comsumptionFee) 
								allowPutUserId = true;	}
						
						if(allowPutUserId){
						Integer userId = projectSupport.getUser().getId();
						if (userTotleAmount.containsKey(userId))
							userTotleAmount
									.put(userId,
											userTotleAmount.get(userId)
													+ totAmount);
						else
							userTotleAmount.put(userId, totAmount);
						
						userIds.put(userId, projectSupport.getProject().getId());
						userNames.put(userId,  projectSupport.getUser().getName());
						}
			    	}
			    }
			    else if (projectIds.contains(projectSupport.getProject().getId())&&projectSupport.getProject().getStatus() == Project.PROJECT_STATUS_ING) {
					Integer totAmount = projectSupport.getProduct().getPrice()
							* projectSupport.getProductAmount();
					Boolean allowPutUserId = false;	
					
					if(projectCategoryIds.contains(0))
					{	if (totAmount >= comsumptionFee) 
							allowPutUserId = true;
					}
					else
						{if ( projectCategoryIds.contains(projectSupport.getProject().getProjectCategory().getId())&& totAmount >= comsumptionFee) 
							allowPutUserId = true;	}
					
					
					if(allowPutUserId){
					Integer userId = projectSupport.getUser().getId();
					if (userTotleAmount.containsKey(userId))
						userTotleAmount
								.put(userId,
										userTotleAmount.get(userId)
												+ totAmount);
					else
						userTotleAmount.put(userId, totAmount);
					
					userIds.put(userId, projectSupport.getProject().getId());
					userNames.put(userId,  projectSupport.getUser().getName());
					}
				}
			}
		}
		
		Map<Integer,List<String>> userProjectIds = new HashMap<Integer,List<String>>();
		List<String> userName = new ArrayList<String>();
		List<String> userListIds = new ArrayList<String>();
		List<String> projectListId = new ArrayList<String>();
		if(userTotleAmount!=null)
		for(Integer userId:userTotleAmount.keySet())
		{
			if(userTotleAmount.get(userId)>=comsumptionTotleFee)
			{				
				String Id = String.valueOf(userId);
				userListIds.add(Id);
				userName.add(userNames.get(userId));
				projectListId.add(String.valueOf(userIds.get(userId)));
			}
		}		
		userProjectIds.put(1, userListIds);
		userProjectIds.put(2, userName);
		userProjectIds.put(3, projectListId);
		return userProjectIds;
	}

	@Override
	public Long sumProductAmount(int projectId,int userId,int productId) {
		
		return projectSupportDao.countProductAmount(projectId, userId,productId);
	}

}
